package com.guldan.jaina.manager;

import java.util.ArrayList;
import java.util.List;

import com.guldan.core.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.projecttag.ProjectTagDTO;
import com.guldan.jaina.dto.projecttag.ProjectTagSearchDTO;
import com.guldan.jaina.utils.ProjectTagDomainConvertUtils;
import com.guldan.mapper.db.entity.ProjectTag;
import com.guldan.mapper.db.entity.ProjectTagExample;
import com.guldan.mapper.db.mapper.ProjectTagMapperExt;

/**
 * Created by Silas.
 * Date: 2016/11/7
 * Time: 22:55
 */
@Service
public class ProjectTagManager extends BaseManager {

    @Autowired
    private ProjectTagMapperExt projectTagMapperExt;

    //获取单个项目标签信息
    public ProjectTagDTO get(Integer projectTagId) {
        return ProjectTagDomainConvertUtils.convert(projectTagMapperExt.selectByPrimaryKey(projectTagId));
    }

    //判断某个标签是否已经存在
    private Boolean checkExist(String tagName) {
        ProjectTagExample example = new ProjectTagExample();
        ProjectTagExample.Criteria criteria = example.createCriteria();
        criteria.andProjectTagNameEqualTo(tagName);
        criteria.andIsDeleteEqualTo(false);
        int count = projectTagMapperExt.countByExample(example);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    //新增/更新单个项目标签，传入：ProjectTagDTO
    public ProjectTagDTO save(ProjectTagDTO dto) throws BusinessException{


    	ProjectTag entity;
        if (dto.getProjectTagId() != null && dto.getProjectTagId() > 0) {
            entity = projectTagMapperExt.selectByPrimaryKey(dto.getProjectTagId());
            entity.setProjectTagName(dto.getProjectTagName());
            entity.setRemark(dto.getRemark());

            entity.setDisabled(dto.getDisabled());
            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(dto.getModifier());
            projectTagMapperExt.updateByPrimaryKeySelective(entity);
        } else {

            //判断是否存存在
            if (checkExist(dto.getProjectTagName())) {
                throw new BusinessException("标签已存在");
            }
            entity = new ProjectTag();
            entity.setProjectTagName(dto.getProjectTagName());
            entity.setRemark(dto.getRemark());

            entity.setDisabled(dto.getDisabled());
            entity.setCreator(dto.getModifier());
            entity.setCreateTime(DateUtil.getCurrentTime());
            projectTagMapperExt.insertSelective(entity);
        }
        dto = ProjectTagDomainConvertUtils.convert(entity);
        return dto;
    }

    //删除单个项目标签信息
    public Boolean delete(Integer projectTagId) {
    	ProjectTag entity = projectTagMapperExt.selectByPrimaryKey(projectTagId);
        entity.setIsDelete(true);
        return projectTagMapperExt.updateByPrimaryKeySelective(entity) > 0;
    }

    //不分页全部查询项目标签信息列表
    public List<ProjectTagDTO> all(ProjectTagSearchDTO projectTagSearchDTO) {
    	ProjectTagExample example = buildSearchExample(projectTagSearchDTO);
        List<ProjectTagDTO> data = new ArrayList<ProjectTagDTO>();
        List<ProjectTag> list = projectTagMapperExt.selectByExample(example);
        for (ProjectTag entity : list) {
        	ProjectTagDTO dto = ProjectTagDomainConvertUtils.convert(entity);
            data.add(dto);
        }
        return data;
    }    
    //build查询Example
    private ProjectTagExample buildSearchExample(ProjectTagSearchDTO search) {
    	ProjectTagExample example = new ProjectTagExample();
    	ProjectTagExample.Criteria criteria = example.createCriteria();

        if (search.getProjectTagId() != null && search.getProjectTagId() > 0) {
            criteria.andProjectTagIdEqualTo(search.getProjectTagId());
        }
        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("project_tag_id asc");
        }
        return example;
    }

    
}
